<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>在 Vue Cli 3 中使用 px2rem | Storm</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="# 安装 postcss-px2rem1yarn add postcss-px2rem # 在 vue.config.js 中添加配置1234567891011121314151617const px2rem = require(&apos;postcss-px2rem&apos;)const postcss = px2rem(&amp;#123;  remUnit: 32 	//基准大小 baseSize，需要和rem.j">
<meta name="keywords" content="Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="在 Vue Cli 3 中使用 px2rem">
<meta property="og:url" content="https://storm4542.github.io/archives/18e53f06.html">
<meta property="og:site_name" content="Storm">
<meta property="og:description" content="# 安装 postcss-px2rem1yarn add postcss-px2rem # 在 vue.config.js 中添加配置1234567891011121314151617const px2rem = require(&apos;postcss-px2rem&apos;)const postcss = px2rem(&amp;#123;  remUnit: 32 	//基准大小 baseSize，需要和rem.j">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-08-25T09:08:27.176Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="在 Vue Cli 3 中使用 px2rem">
<meta name="twitter:description" content="# 安装 postcss-px2rem1yarn add postcss-px2rem # 在 vue.config.js 中添加配置1234567891011121314151617const px2rem = require(&apos;postcss-px2rem&apos;)const postcss = px2rem(&amp;#123;  remUnit: 32 	//基准大小 baseSize，需要和rem.j">
  
    <link rel="alternative" href="https://storm4542.github.io/atom.xml" title="Storm" type="application/atom+xml">
  
  
    <link rel="icon" href="https://storm4542.github.io/favicon.png">
  
  <link href="static/css/css.css" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="static/css/style.css">
  

</head></html>
<body>
  <div id="container">
    <div id="wrap">
      <div class="outer">
        <section id="main"><article id="post-在 Vue Cli 3 中使用 px2rem" class="article article-type-post" itemscope="" itemprop="blogPost">
  
  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      在 Vue Cli 3 中使用 px2rem
    </h1>
  

      </header>
    
    <div class="article-meta">
      <a href="" class="article-date">
  <time datetime="2018-08-25T07:16:43.000Z" itemprop="datePublished">2018-08-25</time>
</a>
      
    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h4 id="安装-postcss-px2rem"><a href="#安装-postcss-px2rem" class="headerlink" title="# 安装 postcss-px2rem"></a># 安装 postcss-px2rem</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add postcss-px2rem</span><br></pre></td></tr></table></figure>
<h4 id="在-vue-config-js-中添加配置"><a href="#在-vue-config-js-中添加配置" class="headerlink" title="# 在 vue.config.js 中添加配置"></a># 在 vue.config.js 中添加配置</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> px2rem = <span class="built_in">require</span>(<span class="string">'postcss-px2rem'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> postcss = px2rem(&#123;</span><br><span class="line">  remUnit: <span class="number">32</span> 	<span class="comment">//基准大小 baseSize，需要和rem.js中相同</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  css: &#123;</span><br><span class="line">    loaderOptions: &#123;</span><br><span class="line">      postcss: &#123;</span><br><span class="line">        plugins: [</span><br><span class="line">          postcss</span><br><span class="line">        ]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="添加-rem-js"><a href="#添加-rem-js" class="headerlink" title="# 添加 rem.js"></a># 添加 rem.js</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 基准大小</span></span><br><span class="line"><span class="keyword">const</span> baseSize = <span class="number">32</span></span><br><span class="line"><span class="comment">// 设置 rem 函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setRem</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 当前页面宽度相对于 750 宽的缩放比例，可根据自己需要修改。</span></span><br><span class="line">  <span class="keyword">const</span> scale = <span class="built_in">document</span>.documentElement.clientWidth / <span class="number">750</span></span><br><span class="line">  <span class="comment">// 设置页面根节点字体大小</span></span><br><span class="line">  <span class="built_in">document</span>.documentElement.style.fontSize = baseSize * <span class="built_in">Math</span>.min(scale, <span class="number">2</span>) + <span class="string">'px'</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 初始化</span></span><br><span class="line">setRem()</span><br><span class="line"><span class="comment">// 改变窗口大小时重新设置 rem</span></span><br><span class="line"><span class="built_in">window</span>.onresize = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  setRem()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="引入-rem-js"><a href="#引入-rem-js" class="headerlink" title="# 引入 rem.js"></a># 引入 rem.js</h4><ul>
<li><p>方法一：直接在 index.html 中写入语句</p>
</li>
<li><p>方法二：将 rem.js 放入 public 文件夹 ， 在 main.js 中添加以下语句</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">;(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> rem = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>);</span><br><span class="line">  rem.src = <span class="string">'./rem.js'</span>;</span><br><span class="line">  <span class="built_in">document</span>.body.appendChild(rem)</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>
</li>
</ul>

      

      
        
    </div>
  </div>
  
    
<nav id="article-nav">
  
    <a href="599cc31e.html" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          如何在mpvue中使用第三方UI组件
        
      </div>
    </a>
  
  
    <a href="b3afd273.html" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Array--方法</div>
    </a>
  
</nav>

  
</article>


<section id="comments">
  <link rel="stylesheet" href="static/css/default.css">
  <script src="static/js/gitment.browser.js"></script>
  <script>
    var gitment = new Gitment({
      owner: 'your github account',
      repo: 'your repo',
      oauth: {
        client_id: 'your client_id',
        client_secret: 'your client_secret',
      },
    })
    gitment.render('comments')
  </script>
</section>

</section>
        <aside id="sidebar">
  <nav class="menus">
  	<ul>
  		<li><a href="index.html"><i class="icon icon-home"></i></a></li>
  		
			<li><a href="index1.html"><i class="icon icon-fenlei"></i></a></li>
  		
  		
			<li><a href="index2.html"><i class="icon icon-tag"></i></a></li>
  		
  		
  			<li><a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer"><i class="icon icon-github"></i></a></li>
  		
  	</ul>
  </nav>
  <a id="go-top" href="#"><i class="icon icon-up"></i></a>
</aside>

      </div>
      <footer id="footer">
  
	<div id="footer-info" class="inner">
	  &copy; 2019 Storm 
	  - Powered by <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Hexo</a>
	  - Theme <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Jane</a>
	</div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="index.html" class="mobile-nav-link">Home</a>
  
    <a href="index1.html" class="mobile-nav-link">Archives</a>
  
    <a href="index2.html" class="mobile-nav-link">Tag</a>
  
    <a href="javascript:;" class="mobile-nav-link" rel="external nofollow noopener noreferrer" target="_blank">Github</a>
  
</nav>
    
<script>
  var disqus_shortname = 'storm';
  
  var disqus_url = 'https://storm4542.github.io/archives/18e53f06.html';
  
  (function(){
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>


<script src="static/js/jquery.min.js"></script>


  <link rel="stylesheet" href="static/css/jquery.fancybox.css">
  <script src="static/js/jquery.fancybox.pack.js"></script>


<script src="static/js/script.js"></script>

  </div>
</body>
